﻿using System.Web;
using Sissonix.ExtRest;
using Sissonix.ExtRest.Processors;
using Sissonix.ExtRest.Exception;
using Sissonix.ExtRest.WADL;

namespace Sissonix.ExtRest.RestManager.Processors
{
    /// <summary>
    /// This is a base class that provides common functionality for 
    /// all classes that will handle incoming URI's.
    /// </summary>
    class BaseRestProcessor : IExtRestProcessor
    {
        /// <summary>
        /// This is the main method that will be called by the
        /// RequestProcessor.  It must be defined by the class that
        /// inherits from MasterRestProcessor.
        /// </summary>
        /// <param name="context">The context.</param>
        public void ProcessRequest(WADLContext context)
        {
            HttpRequest req = HttpContext.Current.Request;
            HttpMethods.Method currentMethod = HttpMethods.ParseHttpMethodType(req.RequestType);
            try
            {
                switch (currentMethod)
                {
                    case HttpMethods.Method.DELETE:
                        ProcessDelete(context); break;
                    case HttpMethods.Method.GET:
                        ProcessGet(context); break;
                    case HttpMethods.Method.POST:
                        ProcessPost(context); break;
                    case HttpMethods.Method.PUT:
                        ProcessPut(context); break;
                    case HttpMethods.Method.HEAD:
                    case HttpMethods.Method.OPTIONS:
                    case HttpMethods.Method.NotHandled:
                    default:
                        break;
                }
            }
            catch (RestException e)
            {
            }
        }

        public virtual void ProcessDelete(WADLContext context)
        {
            throw new ExtRest.Exception.RestException("DELETE action not allow for this URL.");
        }
        public virtual void ProcessGet(WADLContext context)
        {
            throw new ExtRest.Exception.RestException("GET action not allow for this URL.");
        }
        public virtual void ProcessPost(WADLContext context)
        {
            throw new ExtRest.Exception.RestException("POST action not allow for this URL.");
        }
        public virtual void ProcessPut(WADLContext context)
        {
            throw new ExtRest.Exception.RestException("PUT action not allow for this URL.");
        }
        public virtual void ProcessOther(WADLContext context)
        {
            throw new ExtRest.Exception.RestException("Non DELETE/GET/POST/PUT action not allow for this URL.");
        }
    }
}